package com.example.etwzgzxt.view.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;

import com.baidu.mapapi.SDKInitializer;
import com.example.etwzgzxt.R;
import com.example.etwzgzxt.service.PostTask;
import com.example.etwzgzxt.view.fragment.DeviceFragment;
import com.example.etwzgzxt.view.fragment.MainFragment;
import com.example.etwzgzxt.view.fragment.ProfileFragment;
import com.example.etwzgzxt.view.fragment.SOSFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private MainFragment mMainFragment;
    private SOSFragment mSOSFragment;
    private RelativeLayout rel1,rel2,rel3,rel4;
    private FragmentManager mFragmentManager;
    private DeviceFragment mDeviceFragment;
    private ProfileFragment ProfileFragment;
    Fragment[] fragments;
    int curruntpage=0;
    public PostTask pservice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        rel1=(RelativeLayout)findViewById(R.id.re_home);
        rel2=(RelativeLayout)findViewById(R.id.re_device);
        rel3=(RelativeLayout)findViewById(R.id.re_sos);
        rel4=(RelativeLayout)findViewById(R.id.re_profile);
        rel1.setOnClickListener(this);
        rel2.setOnClickListener(this);
        rel3.setOnClickListener(this);
        rel4.setOnClickListener(this);
        mFragmentManager=getSupportFragmentManager();
        mSOSFragment =SOSFragment.newInstance("","");
        mMainFragment= MainFragment.newInstance("","");

        mDeviceFragment=new DeviceFragment();
        ProfileFragment=new ProfileFragment();
        fragments = new Fragment[] {mMainFragment,mDeviceFragment,mSOSFragment,ProfileFragment};
        mFragmentManager.beginTransaction()
                .add(R.id.fragment_container,mMainFragment)
                .add(R.id.fragment_container,mDeviceFragment)
                .add(R.id.fragment_container,mSOSFragment)
                .add(R.id.fragment_container,ProfileFragment)
                .hide(mSOSFragment)
                .hide(ProfileFragment)
                .hide(mDeviceFragment)//.hide(messageFragment)
              .show(mMainFragment).commit();

        Intent intent = new Intent(MainActivity.this,
                PostTask.class);
        startService(intent);
        bindService(intent, conn, Context.BIND_AUTO_CREATE);

    }

    @Override
    public void onClick(View v) {
        int index=0;
        switch (v.getId())
        {
         case R.id.re_home:index=0; break;
            case R.id.re_device:index=1;; break;
            case R.id.re_sos:index=2; break;
            case R.id.re_profile:index=3; break;

        }



        if (curruntpage != index) {
            FragmentTransaction fragmentTransaction = mFragmentManager
                    .beginTransaction();

            if (!fragments[index].isAdded()) {
                fragmentTransaction.add(R.id.fragment_container, fragments[index]);
            }
            fragmentTransaction.hide(fragments[curruntpage]).show(fragments[index]).commit();
            curruntpage=index;
        }
    }
    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            pservice=((PostTask.MyBinder)service).getService();
            listenpoi();
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(conn!=null)
        unbindService(conn);
    }
    public void listenpoi(){
        new Thread(new Runnable() {

            @Override
            public void run() {
                while(true){

               /*     if( pservice.indanger)
                    {
                        FragmentTransaction fragmentTransaction = mFragmentManager
                                .beginTransaction();

                        if (!fragments[2].isAdded()) {
                            fragmentTransaction.add(R.id.fragment_container, fragments[2]);
                        }

                        fragmentTransaction.hide(fragments[curruntpage]).show(fragments[2]).commit();
                        curruntpage=2;
                    }*/

                    try {
                        Thread.sleep(6000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }
}
